﻿Public Class Frm_baseDonne
    Dim path As String = ""
    Public Sub BackUp()
        Try
            Dim cmd As New SqlClient.SqlCommand("BACKUP DATABASE Gestion_Cabinet_Avovat TO DISK ='" & path & "\Gestion_Avocat_" & Date.Now.ToString("dd_MM_yyyy_HH-mm-ss") & ".bak'", Partager.con)
            Partager.open()
            cmd.ExecuteNonQuery()
            Partager.close()
            MsgBox("تم أخذ نسخة إحتياطية لقاعدة البيانات بنجاح", MsgBoxStyle.Information, "نجاح")
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    '-------------------------------------
    Public Sub Restore()
        Try
            Dim cmd As New SqlClient.SqlCommand("USE master;ALTER DATABASE Gestion_Cabinet_Avovat SET SINGLE_USER WITH ROLLBACK IMMEDIATE;" & _
                                                "RESTORE DATABASE Gestion_Cabinet_Avovat FROM DISK ='" & path & "' with replace;", Partager.con)
            Partager.open()
            cmd.ExecuteNonQuery()
            Partager.close()
            MsgBox("تم إستراد النسخة الإحتياطية لقاعدة البيانات بنجاح", MsgBoxStyle.Information, "نجاح")
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub btn_save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_save.Click
        If Fbd.ShowDialog = Windows.Forms.DialogResult.OK Then
            path = Fbd.SelectedPath
            BackUp()
        End If
    End Sub

    Private Sub btn_restor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_restor.Click
        OFD.Filter = "BackUp Files(*.bak)|*.bak"
        If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
            path = OFD.FileName
            Restore()
        End If

    End Sub

    Private Sub Frm_baseDonne_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Partager.SauvegardeAutomatique()
    End Sub
End Class